package com.aaa.medicine.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.aaa.entity.Purchase;
import com.aaa.entity.PurchaseItem;
import com.aaa.entity.User;
import com.aaa.medicine.dao.InventoryMapper;
import com.aaa.medicine.dao.NewPurchaseMapper;
import com.aaa.medicine.dao.PurchaseMapper;
import com.aaa.medicine.fegin.userFeign;
import com.aaa.medicine.service.NewPurchaseService;
import com.aaa.medicine.util.JsonUtil;
import com.aaa.utils.TokenUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;


@Service
public class NewPurchaseServiceImpl extends ServiceImpl<NewPurchaseMapper, Purchase> implements NewPurchaseService{
    @Autowired
    private NewPurchaseMapper newPurchaseMapper;
    @Autowired
    private PurchaseMapper purchaseMapper;
    @Autowired
    private InventoryMapper inventoryMapper;
@Resource
private userFeign userFeign;
    @Override
    public Result<String> gett() {
        //生产ID
        long purchaseId= IdUtil.getSnowflakeNextId();
        String head="CG";
        //拼接
        String concat=head.concat(String.valueOf(purchaseId));
        return new Result<>(2000,"生产成功",concat);
    }
    @Override
    public Result<String> newadd(Map<String, Object> map) {
        HttpServletRequest request= WebUtil.getRequest();
        String token=request.getHeader("token");
        Map<String,Object>tokenData= TokenUtils.getInfo(token);
        String phone=(String)tokenData.get("username");
        User user= userFeign.getByUsername(phone);
        AtomicInteger atomicInteger=new AtomicInteger();
        JsonUtil jsonUtil=new JsonUtil();
        //添加
        Purchase purchase=jsonUtil.mapToObject(map,Purchase.class,"Purchase");
        String purchaseId = purchase.getPurchaseId();
        //状态
        purchase.setStatus("1");
        //添加tock获取到的name
        purchase.setApplyUserName(user.getUserName());
        //添加从openfeign id
        purchase.setApplyUserId(user.getUserId());
        //添加当前时间
        purchase.setCreateTime(LocalDateTime.now());

        int insert= purchaseMapper.insert(purchase);

        if (insert==1){
            atomicInteger.getAndIncrement();
        }
        List<PurchaseItem> purchaseItems=jsonUtil.mapToList(map,PurchaseItem.class,"PurchaseItem");
        purchaseItems.forEach(itme->{
            String s= RandomUtil.randomNumbers(19);
            itme.setItemId(s);
            itme.setPurchaseId(purchase.getPurchaseId());
            int insert1=inventoryMapper.insert(itme);
            if (insert1==1){
                atomicInteger.getAndIncrement();
            }
        });
        return new Result(200,"暂存成功",atomicInteger);
    }

    @Override
    public Result<String> newadd1(Map<String, Object> map) {
        HttpServletRequest request= WebUtil.getRequest();
        String token=request.getHeader("token");
        Map<String,Object>tokenData= TokenUtils.getInfo(token);
        String phone=(String)tokenData.get("username");
        User user= userFeign.getByUsername(phone);
        AtomicInteger atomicInteger=new AtomicInteger();
        JsonUtil jsonUtil=new JsonUtil();
        //添加
        Purchase purchase=jsonUtil.mapToObject(map,Purchase.class,"Purchase");
        String purchaseId = purchase.getPurchaseId();
        //状态
        purchase.setStatus("2");
        //添加tock获取到的name
        purchase.setApplyUserName(user.getUserName());
        //添加从openfeign id
        purchase.setApplyUserId(user.getUserId());
        //添加当前时间
        purchase.setCreateTime(LocalDateTime.now());

        int insert= purchaseMapper.insert(purchase);

        if (insert==1){
            atomicInteger.getAndIncrement();
        }
        List<PurchaseItem> purchaseItems=jsonUtil.mapToList(map,PurchaseItem.class,"PurchaseItem");
        purchaseItems.forEach(itme->{
            String s= RandomUtil.randomNumbers(19);
            itme.setItemId(s);
            itme.setPurchaseId(purchase.getPurchaseId());
            int insert1=inventoryMapper.insert(itme);
            if (insert1==1){
                atomicInteger.getAndIncrement();
            }
        });
        return new Result(200,"暂存成功",atomicInteger);
    }
}
